// pages/news-list/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    host: app.data.api.root_host,
    isCard: true,
    dataList: {},
    id: '',

  },
  changeItemStatus: function(e){
    var index = e.currentTarget.dataset.index;
    var pindex = e.currentTarget.dataset.pindex;
    var dataList = this.data.dataList;
    console.log(pindex)
    if(pindex == -1){
      dataList['commentStatus'] = !dataList['commentStatus'];
      this.setData({
        dataList: dataList
      })
    }else{
      console.log(pindex)
      dataList['commentList'][index]['commentStatus'] = !dataList['commentList'][index]['commentStatus'];
      this.setData({
        dataList: dataList
      })
    }
  },
  openAction: function(e){
    console.log(e);
    var dataList = this.data.dataList;
    dataList.open_status = !dataList.open_status;
    this.setData({
      dataList: dataList
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    });
    this.getList(options.id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  getList: function(id){
    app.data.utils.request({
      url: app.data.api.posts_detail + '/' +id,
      data: {},
      method: 'get',
      success: (data) => {
        this.setData({
          dataList: data,
        })
      }

    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    app.data.utils.request({
      url: app.data.api.post_share + '/' + this.data.id + '/share',
      method: 'post',
      success: (res)=> {
        wx.showToast({
          title: '分享成功',
        })
      }
    })
  },
  zanAction: function(e) {
    var posts_id = e.currentTarget.dataset.posts_id;
    app.data.utils.request({
      url: app.data.api.posts_zan+"/"+posts_id+"/zan",
      data: {
      },
      method: 'post',
      success: (res)=> {
        wx.showToast({
          title: '点赞成功',
        })
        this.getList();
      }
    })
  }
})